package com.smart.controller.sys;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import com.smart.vo.RouterVo;
import com.smart.vo.TreeSelect;
import com.smart.util.Messages;
import com.smart.util.SysUserUtils;
import com.smart.entity.SysMenu;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import com.smart.util.CommonResult;
import com.smart.service.MenuService;
import com.smart.common.annotation.SysLog;
import com.smart.common.enums.BusinessType;
import com.smart.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author lgb
 * @date 2020年6月10日 下午5:26:12
 * Copyright (C) 深圳工勘科技
 * @说明: 系统菜单管理
 **/
@RestController("sysMenuController")
@RequestMapping("/sys/menu")
@Api(tags="Sys-menu=>系统菜单管理")
public class MenuController extends BaseController{

	@Resource
    private MenuService menuService;
	
	@ApiOperation("list => 获取菜单列表")
	@GetMapping("/list")
    public Messages<List<SysMenu>> list(SysMenu menu) {
        List<SysMenu> menus=menuService.selectMenuList(menu,SysUserUtils.getSysUser().getId());
        return Messages.success(menus);
    }
	
	@ApiOperation("getRouters => 获取路由信息")
    @GetMapping("/getRouters")
    public Messages<List<RouterVo>> getRouters() {
		Long userId = SysUserUtils.getSysUser().getId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        List<RouterVo> voList=menuService.buildMenus(menus);
        return Messages.success(voList);
    }
	
	@ApiOperation("treeselect => 获取菜单下拉树列表")
    @GetMapping(value="/treeselect")
    public CommonResult<List<TreeSelect>> treeselect(SysMenu menu){
        List<SysMenu> menus=menuService.selectMenuList(menu,SysUserUtils.getSysUser().getId());
        return CommonResult.success(menuService.buildMenuTreeSelect(menus));
    }
	
	@ApiOperation("create => 新增菜单")
    @SysLog(title="菜单管理",businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public CommonResult<Integer> create(@RequestBody SysMenu menu) {
        int count = menuService.create(menu);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update => 修改菜单")
    @SysLog(title="菜单管理",businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public CommonResult<Integer> update(@RequestBody SysMenu menu) {
        boolean count = menuService.updateById(menu);
        if (count == true) {
            return CommonResult.success(1);
        }
        return CommonResult.failed();
    }
	
	@GetMapping(value="/roleMenuTreeselect/{roleId}")
    @ApiOperation("roleMenuTreeselect/{roleId} => 加载对应角色菜单列表树")
    public CommonResult<Map<String, Object>> roleMenuTreeselect(
    		@PathVariable("roleId") Long roleId) {
        List<SysMenu> menus = menuService.selectMenuList(SysUserUtils.getSysUser().getId());
        Map<String, Object> map = new HashMap<>();
        map.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        map.put("menus", menuService.buildMenuTreeSelect(menus));
        return CommonResult.success(map);
    }
	
	@ApiOperation("{id} => 根据菜单ID获取详细信息")
    @GetMapping("/{id}")
    public CommonResult<SysMenu> info(@PathVariable("id") Long id) {
        SysMenu menus = menuService.findById(id);
        return CommonResult.success(menus);
    }
	
}